package com.alibaba.cloud.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;


/**
 * The Class Builder.
 *
 * @param <T> the generic type
 * @ClassName: Builder
 * @Description:通用的 Builder 模式构建器(适用于所有对象)
 * @author: zhb1316783262@163.com
 * @date: 2020年4月5日下午10:49:02
 */
public class Builder<T> {
    
    /** The instantiator. */
    private final Supplier<T> instantiator;
    
    /** The modifiers. */
    private List<Consumer<T>> modifiers = new ArrayList<>();

    /**
     * Instantiates a new builder.
     *
     * @param instantiator the instantiator
     */
    public Builder(Supplier<T> instantiator) {
        this.instantiator = instantiator;
    }

    /**
     * Of.
     *
     * @param <T> the generic type
     * @param instantiator the instantiator
     * @return the builder
     */
    public static <T> Builder<T> of(Supplier<T> instantiator) {
        return new Builder<>(instantiator);
    }

    /**
     * With.
     *
     * @param <P1> the generic type
     * @param consumer the consumer
     * @param p1 the p 1
     * @return the builder
     */
    public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
        Consumer<T> c = instance -> consumer.accept(instance, p1);
        modifiers.add(c);
        return this;
    }

    /**
     * With.
     *
     * @param <P1> the generic type
     * @param <P2> the generic type
     * @param consumer the consumer
     * @param p1 the p 1
     * @param p2 the p 2
     * @return the builder
     */
    public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
        Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
        modifiers.add(c);
        return this;
    }

    /**
     * With.
     *
     * @param <P1> the generic type
     * @param <P2> the generic type
     * @param <P3> the generic type
     * @param consumer the consumer
     * @param p1 the p 1
     * @param p2 the p 2
     * @param p3 the p 3
     * @return the builder
     */
    public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
        Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
        modifiers.add(c);
        return this;
    }

    /**
     * 1 参数 Consumer.
     *
     * @param <T> the generic type
     * @param <P1> the generic type
     */
    @FunctionalInterface
    public interface Consumer1<T, P1> {
        
        /**
         * Accept.
         *
         * @param t the t
         * @param p1 the p 1
         */
        void accept(T t, P1 p1);
    }

    /**
     * 2 参数 Consumer.
     *
     * @param <T> the generic type
     * @param <P1> the generic type
     * @param <P2> the generic type
     */
    @FunctionalInterface
    public interface Consumer2<T, P1, P2> {
        
        /**
         * Accept.
         *
         * @param t the t
         * @param p1 the p 1
         * @param p2 the p 2
         */
        void accept(T t, P1 p1, P2 p2);
    }

    /**
     * 3 参数 Consumer.
     *
     * @param <T> the generic type
     * @param <P1> the generic type
     * @param <P2> the generic type
     * @param <P3> the generic type
     */
    @FunctionalInterface
    public interface Consumer3<T, P1, P2, P3> {
        
        /**
         * Accept.
         *
         * @param t the t
         * @param p1 the p 1
         * @param p2 the p 2
         * @param p3 the p 3
         */
        void accept(T t, P1 p1, P2 p2, P3 p3);
    }

    /**
     * Builds the.
     *
     * @return the t
     */
    public T build() {
        T value = instantiator.get();
        modifiers.forEach(modifier -> modifier.accept(value));
        modifiers.clear();
        return value;
    }
}
